package org.example.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.SortParameters;
import org.springframework.data.redis.core.BulkMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.query.SortQuery;
import org.springframework.data.redis.core.query.SortQueryBuilder;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController()
public class ListAndSetAndZset {

    @Autowired
    @Qualifier("redisTemplate1")
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * list，set，sorted set元素的排序
     * todo 没生效
     * @param key
     * @return
     */

    @RequestMapping("/sortKey")
    public Long sortKey(String key) {
        SortQuery<String> build = SortQueryBuilder.sort(key).order(SortParameters.Order.DESC).alphabetical(true).limit(0,8).build();
        Long sort = redisTemplate.sort(build, String.valueOf("testzsort"));
        return sort;
    }


    /**
     * 对key进行排序
     * @param key
     * @return
     */
    @RequestMapping("/sort")
    public List<Object> sort(String key) {
        SortQuery<String> build = SortQueryBuilder.sort(key).order(SortParameters.Order.ASC).alphabetical(true).limit(0,8).build();
//        SortQuery<String> build = SortQueryBuilder.sort(key).build();

        return redisTemplate.sort(build);
    }


    /**
     * 对key进行排序加序列化
     * @param key
     * @return
     */
    @RequestMapping("/sortRedisSerializer")
    public List<Object> sortRedisSerializer(String key) {
        SortQuery<String> build = SortQueryBuilder.sort(key).order(SortParameters.Order.DESC).alphabetical(true).limit(0,8).build();
//        new DefaultSortQuery(key, SortParameters.Order.DESC,true,new SortParameters.Range(0,10),"sd",null);
        return redisTemplate.opsForValue().getOperations().sort(build, new Jackson2JsonRedisSerializer<>(Object.class));
    }



    /**
     * 批量映射器
     * todo 匹配规则好像是不生效的
     * @param key
     * @return
     */
    @RequestMapping("/sortBulkMapper")
    public List<Integer> sortBulkMapper(String key) {
        SortQuery<String> defaultSortQuery = SortQueryBuilder.sort(key).order(SortParameters.Order.DESC).get("/d").alphabetical(true).limit(0,8).build();
        defaultSortQuery.getGetPattern();
        BulkMapper<Integer, Object> bulkMapper = tuple -> {
            Integer integer = 120;
            for (int i = 0; i < tuple.size(); i++) {
                if(null!=tuple.get(i)){
                    integer += Integer.parseInt(tuple.get(i).toString());
                }

            }
            return integer;
        };
        List<Integer> result=redisTemplate.sort(defaultSortQuery, bulkMapper);
        return  result;
    }




    /**
     * 批量映射器+序列化
     * @param key
     * @return
     * @param <T>
     */
    @RequestMapping("/sortBulkMapperRedis")
    public <T> List sortBulkMapperRedis(String key) {
        SortQuery<String> defaultSortQuery = SortQueryBuilder.sort(key).order(SortParameters.Order.DESC).get("/d").alphabetical(true).limit(0,8).build();
        BulkMapper<Integer, Object> bulkMapper = tuple -> {
            Integer integer = 120;
            for (int i = 0; i < tuple.size(); i++) {
                if(null!=tuple.get(i)){
                    integer += Integer.parseInt(tuple.get(i).toString());
                }

            }
            return integer;
        };
        return redisTemplate.opsForValue().getOperations().sort(defaultSortQuery, bulkMapper,new Jackson2JsonRedisSerializer<>(Object.class));
    }

}
